﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using supermarket.method;

namespace supermarket
{
    public partial class EditShopForm : Form
    {
        private string shopCode;
        SqlConnection con = new SqlConnection();

        public EditShopForm()
        {
            InitializeComponent();
        }

        public EditShopForm(string shopCode, string shopName, string shopLocation,
                           string shopManager, string shopPhoneNum, string shopOpened, string shopRegion)
        {
            InitializeComponent();

            this.shopCode = shopCode;

            txtShopCode.Text = shopCode;
            txtShopName.Text = shopName;
            txtShopLocation.Text = shopLocation;
            txtShopManager.Text = shopManager;
            txtShopPhoneNum.Text = shopPhoneNum;
            txtShopOpened.Text = shopOpened;
            txtShopRegion.Text = shopRegion;

            txtShopCode.ReadOnly = true;

            // 添加保存按钮点击事件
            btnSave.Click += BtnSave_Click;
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Classgetsql sqlcon = new Classgetsql();
                con = sqlcon.getcon();

                string query = @"UPDATE super.dbo.shop_List 
                               SET shop_name = @shopName, 
                                   shop_location = @shopLocation, 
                                   shop_manager = @shopManager, 
                                   shop_phoneNum = @shopPhoneNum, 
                                   shop_Opened = @shopOpened, 
                                   shop_region = @shopRegion
                               WHERE shop_code = @shopCode";

                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("@shopCode", shopCode);
                    cmd.Parameters.AddWithValue("@shopName", txtShopName.Text);
                    cmd.Parameters.AddWithValue("@shopLocation", txtShopLocation.Text);
                    cmd.Parameters.AddWithValue("@shopManager", txtShopManager.Text);
                    cmd.Parameters.AddWithValue("@shopPhoneNum", txtShopPhoneNum.Text);
                    cmd.Parameters.AddWithValue("@shopOpened", txtShopOpened.Text);
                    cmd.Parameters.AddWithValue("@shopRegion", txtShopRegion.Text);

                    int rowsAffected = cmd.ExecuteNonQuery();
                    if (rowsAffected > 0)
                    {
                        MessageBox.Show("更新成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新失败，未找到对应记录！", "错误",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show($"更新数据时发生错误: {ex.Message}", "错误",
                               MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
